#include "Screen.h"

#include "MachineDependents.h"


Screen::Screen() : BasicDevice(MDConst::I2C_FILE) {
    if(ioctl(deviceFd, I2C_SLAVE_FORCE, I2C_ADDR)) {
        LOG_ERROR("set panel slave addr error!\n");
        return;
    }
}

Screen::~Screen() {

}


void  Screen::show(const std::string* first, const std::string* second, const std::string* third, const std::string* four) {
    std::string::size_type size;
    std::string buffer(SCREEN_WIDTH * SCREEN_HEIGHT, ' ');
    const std::string* pLine = first;
    if (pLine) {
        size = pLine->size();
        if (size > SCREEN_WIDTH) {
            LOG_DEDUG("line is too long:%zd,[%s]", size, pLine->data());
//            assert(size <= SCREEN_WIDTH);
            size = SCREEN_WIDTH;
        }
        buffer.replace(0, size, *pLine);
        // REPEAT_ACTION(i2c_smbus_write_i2c_block_data(deviceFd, FIRST_LINE_OFFSET, pLine->length(), reinterpret_cast<const uint8_t*>(pLine->data())), i2c_smbus_read_byte_data(deviceFd, 0x1F) > 0)
    }
    pLine = second;
    if (pLine) {
        size = pLine->size();
        if (size > SCREEN_WIDTH) {
            LOG_DEDUG("line is too long:%zd,[%s]", size, pLine->data());
//            assert(size <= SCREEN_WIDTH);
            size = SCREEN_WIDTH;
        }
        buffer.replace(SCREEN_WIDTH, size, *pLine);
//        REPEAT_ACTION(i2c_smbus_write_i2c_block_data(deviceFd, FIRST_LINE_OFFSET + 1, pLine->length(), reinterpret_cast<const uint8_t*>(pLine->data())), i2c_smbus_read_byte_data(deviceFd, 0x1F) > 0)

    }
    pLine = third;
    if (pLine) {
        size = pLine->size();
        if (size > SCREEN_WIDTH) {
            LOG_DEDUG("line is too long:%zd,[%s]", size, pLine->data());
//            assert(size <= SCREEN_WIDTH);
            size = SCREEN_WIDTH;
        }
        buffer.replace(SCREEN_WIDTH << 1, size, *pLine);
//        REPEAT_ACTION(i2c_smbus_write_i2c_block_data(deviceFd, FIRST_LINE_OFFSET + 2, pLine->length(), reinterpret_cast<const uint8_t*>(pLine->data())), i2c_smbus_read_byte_data(deviceFd, 0x1F) > 0)
    }
    pLine = four;
    if (pLine) {
        size = pLine->size();
        if (size > SCREEN_WIDTH) {
            LOG_DEDUG("line is too long:%zd,[%s]", size, pLine->data());
//            assert(size <= SCREEN_WIDTH);
            size = SCREEN_WIDTH;
        }
        buffer.replace(SCREEN_WIDTH * 3, size, *pLine);
//        REPEAT_ACTION(i2c_smbus_write_i2c_block_data(deviceFd, FIRST_LINE_OFFSET + 3, pLine->length(), reinterpret_cast<const uint8_t*>(pLine->data())), i2c_smbus_read_byte_data(deviceFd, 0x1F) > 0)
    }
    showFast(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, buffer);
}




